<?php
	session_start();
	require('include/inc.start.php');

// if bad data is passed, we go away from this page
if (!isset($_GET['bookId']) and !is_numeric($_GET['bookId'])){		
 header('Location:buyBooks.php');
}

include("include/connectionParameters.php");

// checking if books exists in database
 $bookId = $_GET['bookId'];
 
     $connection = mysql_connect($host,$user,$pass)
		or die ("Can't connect to server, try again later, please");

	mysql_select_db($database);
	
	
	$query = "select * from Books where Books.BookId='".$bookId."'";
	$result = mysql_query($query,$connection)
		or die ("Problem with adding information, please try again later.");
	
	$book_exists = mysql_num_rows($result)>0;
	
	if (!$book_exists){
		header('Location:buyBooks.php');
	}
	
mysql_close($connection);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Book details</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>

<body>

<?php
    $connection = mysql_connect($host,$user,$pass)
		or die ("Can't connect to server, try again later, please");

	mysql_select_db($database);
	
	
	$query = "select HitCount from Hits,Books where Books.BookId=Hits.Bookid and Hits.BookId='".$bookId."'";
	$result = mysql_query($query, $connection)
		or die ("Problem with adding information, please try again later.");
	$number_of_rows = mysql_num_rows($result);
	$row = mysql_fetch_row($result);
	$hits = $row[0];
	
	// increase hit count
	$hits++;
	
		  if ($number_of_rows >0){
		  // we have record about this book already
		  echo "updating hits count";
		  // updating hits value
		  $query = "UPDATE Hits set HitCount = '".$hits."' where BookId='".$bookId."'";
	$result = mysql_query($query,$connection)
		or die ("Problem with adding information, please try again later.");
		} else {
		 // adding hits value to database
		$query = "insert into `Hits`(`bookId`,`HitCount`) values ('".$bookId."','".$hits."')";
	$result = mysql_query($query,$connection)
		or die ("Problem with adding information, please try again later.");
		}
	
	mysql_close($connection);
  
  echo "Showing details for book with id ".$bookId;
?> 

</body>
</html>
